var Promise = require('es6-promise').Promise;
var commonHeader = function(req, res) {
    res.set("Access-Control-Allow-Origin", '*');
}

module.exports = {
    category: function(req, res, next) {
        commonHeader(req, res);

        Category.find(req.query)
            .then(data => res.end(JSON.stringify({
                status: "success",
                data: data
            })))
            .catch(e => JSON.stringify({
                status: "error",
                error: e
            }))
    },

    category_list: function(req, res, next) {
        commonHeader(req, res);

        var category_name = req.param("category").trim().toLowerCase();
        console.log("category_name", category_name);
        var model = null;
        var json = {};

        // 获取当前分类信息
        Category.findOne({
                dir: category_name
            })
            .then(category => {
                if (category) {
                    var model_str = Util.firstUper(category.model);
                    eval("model = " + model_str);
                    if (!model) {
                        throw "No Found Category Model: " + model_str
                    }
                    json.category = category;
                } else {
                    throw "No Found Category: " + category_name;
                }
            })

        // 获取同种大类下的所有分类
        .then(() => {
                if (req.query.disable_some_model_categorys) {
                    return
                }
                return Category.getTree({
                    model: json.category.model
                })
            })
            .then(some_model_categorys => {
                json.some_model_categorys = some_model_categorys;
            })

        // 获取分页列表
        .then(() => {
                if (json.category.model === "page" || req.disable_page_data) {
                    return
                }
                var current_page = req.query['p'] || req.query['page'] || 1;
                var pagesize = req.query['n'] || req.query['num'] || req.query['pagesize'];
                var pageContion = {
                    sort: "listorder ASC",
                    limit: pagesize || json.category.pagesize || 10
                };
                pageContion.where = {
                    category: json.category.id
                };

                return Pagination(model, {
                    current_page: current_page
                }, pageContion);
            })
            .then(page_data => {
                json.page_data = page_data;
            })

        // 获取热门数据
        .then(() => {
                if (req.query.disable_hot_records) {
                    return
                }
                return model.find({
                    limit: 5
                });
            })
            .then(hot_records => {
                json.hot_records = hot_records || [];
            })

        .then(() => res.end(JSON.stringify({
            status: "success",
            data: json
        })))

        .catch(e => {
            console.error(e);
            res.end(JSON.stringify({
                status: "error",
                error: e.message
            }))
        });
    },

    other: function(req, res, next) {
        commonHeader(req, res);

        var other_name = req.param("other").trim();
        console.log("other_name", other_name);
        var model;
        // 寻找模块 
        Promise.resolve(null)
            .then(() => {

                var model_str = Util.firstUper(other_name);
                eval("model = " + model_str);
                if (!model || typeof model.find !== "function") {
                    throw "No Found  Model: " + model_str
                }
            })

        // 获取模块数据
        .then(() => model.find(req.query))
            .then(data => res.end(JSON.stringify({
                status: "success",
                data: data
            })))

        .catch(e => JSON.stringify({
            status: "error",
            error: e
        }))
    }
};